<?php

namespace common\models\member;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%member_bond}}".
 *
 * @property int $id
 * @property int $member_id 会员ID
 * @property int $user_id 用户ID
 * @property int $type_id 会员类型ID
 * @property string $price 金额
 * @property string $order_no 交易订单号
 * @property int $trade_id 交易ID
 * @property string $out_trade_no 交易订单号
 * @property int $pay_time 支付时间
 * @property int $expired_at 支付失效时间
 * @property int $status 订单状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class MemberBond extends \common\db\ActiveRecord implements \common\base\BussinessTradeInterface {

    const STATUS_DEFAULT = 0; //默认
    const STATUS_ACTIVE = 10; //已提交，待付款
    const STATUS_PAID = 20; //已付款
    const EXPIRED_DURTION = 1800;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%member_bond}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['member_id', 'user_id', 'type_id', 'trade_id', 'pay_time', 'expired_at', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['price'], 'number'],
            [['order_no'], 'required'],
            [['order_no', 'out_trade_no'], 'string', 'max' => 32],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'member_id' => '会员ID',
            'user_id' => '用户ID',
            'type_id' => '会员类型ID',
            'price' => '金额',
            'order_no' => '交易订单号',
            'trade_id' => '交易ID',
            'out_trade_no' => '交易订单号',
            'pay_time' => '支付时间',
            'expired_at' => '支付失效时间',
            'status' => '订单状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    /**
     * 关联会员
     */
    public function getMember() {
        return $this->hasOne(Member::className(), ['id' => 'member_id']);
    }

    /**
     * 关联会员
     */
    public function getType() {
        return $this->hasOne(MemberType::className(), ['id' => 'type_id']);
    }

    public function getCountdown() {
        $duled = 0; //time() - $this->created_at;
        $seconds = self::EXPIRED_DURTION - $duled;
        if ($seconds > 0) {
            return $seconds;
        }
        return 0;
    }

    public function getIsExpired() {
        if ($this->status <= self::STATUS_ACTIVE && $this->getCountdown() <= 0) {
            return true;
        }
        return false;
    }

    public function getIsPay() {
        if ($this->status === self::STATUS_PAID && $this->pay_time >= $this->created_at && $this->out_trade_no) {
            return true;
        }
        return false;
    }

    public function getAmount() {
        return $this->price;
    }

    public function setPaying() {
        if ($this->status == self::STATUS_DEFAULT) {
            $this->status = static::STATUS_ACTIVE;
        }
    }

    public function generateNo() {
        $from = $this->type_id;
        $pcode = 'B' . rand(0, 9);
        $date = date('Ymd');
        $rules = [
            'type' => $pcode,
            'from' => str_pad(dechex($from), 2, "0", STR_PAD_LEFT),
            'date' => substr($date, 2),
        ];
        do {
            $micro = round((microtime(true) - strtotime($date)) * 10000);
            $rules['micro'] = str_pad($micro, 9, 0, STR_PAD_RIGHT);
            $tn = implode($rules);
            $record = static::find()->where(['order_no' => $tn])->exists();
        } while ($record);
        return $this->order_no = $tn;
    }

    public function getTradeDescription() {
        return $this->type ? $this->type->name : '会员保证金';
    }

    public function getTradeTitle() {
        return '优越会员保证金';
    }

    public function getSuccessMessage() {
        return [
            'title' => '恭喜您成为优越定制会员！',
            'message' => '恭喜您成为优越定制会员',
            'cover' => Url::attach('/assets/congratulate.png'),
            'button' => [
                'text' => '开启您的会员之旅',
                'color' => '#f5863c',
            ]
        ];
    }

}
